package com.shaco.heroproject.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.shaco.heroproject.R;
import com.shaco.heroproject.entity.GoodInforResult;
import com.shaco.heroproject.volley.VolleySingleton;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by dllo on 15/11/3.
 */
public class GoodInforActivity extends Activity implements View.OnClickListener {
    private TextView good_infor_nameTv, good_infor_priceTv, good_infor_total_priceTv,
            good_infor_skillTv;
    private ImageView good_infor_Iv;
    private VolleySingleton queue;
    private Button activity_good_backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good_infor);
        initView();
        initData();
    }

    private void initView() {
        good_infor_nameTv = (TextView) findViewById(R.id.good_infor_nameTv);
        good_infor_priceTv = (TextView) findViewById(R.id.good_infor_priceTv);
        good_infor_total_priceTv = (TextView) findViewById(R.id.good_infor_total_priceTv);
        good_infor_skillTv = (TextView) findViewById(R.id.good_infor_skillTv);
        activity_good_backBtn= (Button) findViewById(R.id.activity_good_backBtn);
        activity_good_backBtn.setOnClickListener(this);
        good_infor_Iv = (ImageView) findViewById(R.id.good_infor_Iv);


    }

    private void initData() {
        String id = getIntent().getStringExtra("id");
//        queue = Volley.newRequestQueue(this);
        queue = VolleySingleton.getVolleyIntance();
        String url="http://lol.data.shiwan.com/lolItemInfo/?id="+id;
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                GoodInforResult gr = gson.fromJson(response, GoodInforResult.class);
                good_infor_nameTv.setText(gr.getResult().getName());
                good_infor_total_priceTv.setText("价格: "+gr.getResult().getPrice());
                good_infor_skillTv.setText(gr.getResult().getSkill());

                String imgUrl=gr.getResult().getIcon();
                Log.i("shaco",imgUrl);
                getBitmap(imgUrl, good_infor_Iv);

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("Accept-Encoding", "");
                return map;
            }

        };
//        queue.add(request);
        queue.addRequest(request);
    }

    /**
     * 通过volley请求图片
     * **/
    public void getBitmap(String url, final ImageView good_infor_Iv) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(good_infor_Iv, R.drawable.ic_launcher, R.drawable.ic_launcher);
        VolleySingleton.getVolleyIntance().getImageLoader().get(url, listener);
        VolleySingleton.getVolleyIntance().getImage(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                good_infor_Iv.setImageBitmap(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
//        queue = VolleySingleton.getVolleyIntance();
//        ImageRequest request = new ImageRequest(url,
//                new Response.Listener<Bitmap>() {
//                    @Override
//                    public void onResponse(Bitmap response) {
//                        // 将返回的bitmap图片对象设置到图片组件中
//                        good_infor_Iv.setImageBitmap(response);
//                    }
//                }, 300, 300, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError error) {
//                // 打印错误信息
//                error.printStackTrace();
//            }
//        }){
//            @Override
//            public Map<String, String> getHeaders()
//                    throws AuthFailureError {
//                HashMap<String, String> map = new HashMap<String, String>();
//                map.put("Accept-Encoding", "");
//                return map;
//            }
//        };
////        queue.add(request); // 将本次图片请求加入到请求列队中
//        queue.addRequest(request);

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.activity_good_backBtn:
                finish();
                break;
        }
    }
}
